package com.guyu.app.common.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.guyu.app.common.biz.service.DistrictService;
import com.guyu.app.common.mapper.ds.DistrictMapper;
import com.guyu.app.common.pojo.domain.ds.DistrictDO;
import com.guyu.app.common.pojo.domain.ds.table.DistrictTableDef;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class DistrictServiceImpl extends ServiceImpl<DistrictMapper, DistrictDO> implements DistrictService {


    @Override
    public int insertOrUpdate(DistrictDO districtDO) {
        return mapper.insertOrUpdate(districtDO);
    }

    @Override
    public DistrictDO findByAdcode(String adcode) {
        if (StrUtil.isBlank(adcode)) {
            return null;
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.ADCODE.eq(adcode))
                .limit(1)
                .one();
    }

    @Override
    public List<DistrictDO> findByAdcodeIn(Collection<String> codes) {
        if (Objects.isNull(codes)) {
            return Lists.newArrayList();
        }
        codes = codes.stream().filter(StrUtil::isNotBlank).collect(Collectors.toSet());
        if (CollUtil.isEmpty(codes)) {
            return Lists.newArrayList();
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.ADCODE.in(codes))
                .list();
    }

    @Override
    public List<DistrictDO> findByParentCode(String parentCode) {
        if (StrUtil.isBlank(parentCode)) {
            return Lists.newArrayList();
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.PARENT_CODE.eq(parentCode))
                .list();
    }

    @Override
    public List<DistrictDO> findByParentCodeIn(Collection<String> codes) {
        if (Objects.isNull(codes)) {
            return Lists.newArrayList();
        }
        codes = codes.stream().filter(StrUtil::isNotBlank).collect(Collectors.toSet());
        if (CollUtil.isEmpty(codes)) {
            return Lists.newArrayList();
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.PARENT_CODE.in(codes))
                .list();
    }

    @Override
    public List<DistrictDO> findByNameLike(String name) {
        if (StrUtil.isBlank(name)) {
            return Lists.newArrayList();
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.NAME.like(name))
                .limit(100)
                .list();
    }

    @Override
    public DistrictDO findByNameAndLevel(String name, Integer level) {
        if (StrUtil.isBlank(name) || Objects.isNull(level)) {
            return null;
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.NAME.eq(name))
                .and(DistrictTableDef.DISTRICT.LEVEL.eq(level))
                .limit(1)
                .one();
    }

    @Override
    public DistrictDO findByParentCodeAndName(String parentCode, String name) {
        if (StrUtil.isBlank(name) || StrUtil.isBlank(parentCode)) {
            return null;
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.PARENT_CODE.eq(parentCode))
                .and(DistrictTableDef.DISTRICT.NAME.eq(name))
                .limit(1)
                .one();
    }

    @Override
    public List<DistrictDO> findByLevelIn(Collection<Integer> levels) {
        if (Objects.isNull(levels)) {
            return Lists.newArrayList();
        }
        levels = levels.stream().filter(Objects::nonNull).collect(Collectors.toSet());
        if (CollUtil.isEmpty(levels)) {
            return Lists.newArrayList();
        }
        return queryChain().select()
                .where(DistrictTableDef.DISTRICT.LEVEL.in(levels))
                .list();
    }
}
